package com.ebelter.temperature.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.temperature.R;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class THostoryListAdapter extends BaseAdapter {


    //private static final String TAG = "THostoryListAdapter";
    private Context mContext;


    public THostoryListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<GetTempertureData.ResultDataBean.DataBean> getData() {
        return data;
    }

    private List<GetTempertureData.ResultDataBean.DataBean> data;

    public void setData(List<GetTempertureData.ResultDataBean.DataBean> data) {

        this.data=data;
        notifyDataSetChanged();
    }

    public void addData(List<GetTempertureData.ResultDataBean.DataBean> datas) {
        if (data==null){
           data=new ArrayList<>();
        }
       data.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Vh vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.t_history_item,null);
            vh=new Vh();
            vh.date_tv=convertView.findViewById(R.id.t_his_toptime_tv);
            vh.time_tv=convertView.findViewById(R.id.t_his_time2_tv);
            vh.value_tv=convertView.findViewById(R.id.t_his_value_tv);
            convertView.setTag(vh);
        }else {
            vh= (Vh) convertView.getTag();
        }
        GetTempertureData.ResultDataBean.DataBean bean=data.get(position);



        Date date=TimeUtils.parseFormatter1Time(bean.getTestDate());
        vh.date_tv.setText(TimeUtils.formatDate_nyr(mContext,date.getTime()));
        vh.time_tv.setText(TimeUtils.formatTime_shf(mContext,date.getTime()));
        vh.value_tv.setText(bean.getTemperature()+" ℃");

        updateView(position,vh.date_tv);

        return convertView;
    }





    private void updateView(int position,View topView){
        if (data==null||data.get(position)==null){
            return;
        }

        String lastTime = null;//上一个时间
        String currentTime = null;//当前时间
       // String nextTime = null;//下一个时间

        try {
            currentTime=data.get(position).getTestDate().substring(0,10);
            GetTempertureData.ResultDataBean.DataBean last=data.get(position-1);
            lastTime=last.getTestDate().substring(0,10);
        }catch (Exception e){
            e.printStackTrace();
        }
//        try {
//            GetTempertureData.ResultDataBean.DataBean next=data.get(position+1);
//            nextTime=next.getTestDate().substring(0,10);
//        }catch (Exception e){
//            e.printStackTrace();
//        }


        if (currentTime!=null&& TextUtils.equals(lastTime,currentTime)){
            //当前时间与上一条时间是同一天
            ViewUtils.goneView(topView);
        }else {
            ViewUtils.displayView(topView);
        }

//        if (currentTime!=null&&TextUtils.equals(currentTime,nextTime)){
//            //当前时间与下一条时间是同一天
//            ViewUtils.displayView(lineView);
//            linearLayout.setBackgroundResource(R.drawable.sp_white_lt6_rt6);
//        }else {
//            ViewUtils.goneView(lineView);
//            linearLayout.setBackgroundResource(R.drawable.sp_white_all6);
//        }

//        if (currentTime!=null&&TextUtils.equals(currentTime,nextTime)&&TextUtils.equals(lastTime,currentTime)){
//            //当前时间与上，下一条时间是同一天
//            linearLayout.setBackgroundResource(R.drawable.sp_white);
//        }
//        if (currentTime!=null&&TextUtils.equals(lastTime,currentTime)&&!TextUtils.equals(currentTime,nextTime)){
//            //当前时间与上同一天，与下一条时间不是同一天
//            linearLayout.setBackgroundResource(R.drawable.sp_white_lb6_rb6);
//        }






    }





    private class Vh{
        TextView date_tv;
        TextView time_tv;
        TextView value_tv;
    }
}
